//
//  DLAppDelegate.m
//  UnicodeTool
//
//  Created by David Lee on 14-3-14.
//  Copyright (c) 2014年 Bluemobi.inc. All rights reserved.
//

#import "DLAppDelegate.h"

@implementation DLAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSFont *font = [NSFont fontWithName:@"Menlo" size:11];
    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
    [attributes setObject:font forKey:NSFontAttributeName];

    [_textView setTypingAttributes:attributes];
}

- (BOOL)windowShouldClose:(id)sender
{
    [[NSApplication sharedApplication] terminate:self];
    return YES;
}

static inline BOOL isCharOrNum(char c)
{
    if (c > 47 && c < 58) {
        // num
        return YES;
    }
    if (c > 64 && c < 91) {
        // upper char
        return YES;
    }
    if (c > 96 && c < 123) {
        // lower char
        return YES;
    }
    return NO;
}

- (NSString *)transMixString:(NSString *)mixStr
{
    NSMutableString *result = [[NSMutableString alloc] init];
    NSInteger length = [mixStr length];
    for (int index = 0; index < length; index ++ ) {
        [result appendString:[mixStr substringWithRange:NSMakeRange(index, 1)]];
        
        if (index + 5 > length) {
            continue;
        }
        char c = [mixStr characterAtIndex:index];
        char c1 = [mixStr characterAtIndex:index+1];
        
        BOOL con = YES;
        if (c == '\\' && (c1 == 'u' || c1 == 'U')) {
            for (int iIndex = index + 2; iIndex < index+6; iIndex ++ ) {
                char cChar = [mixStr characterAtIndex:iIndex];
                if (isCharOrNum(cChar)) {
                    continue;
                } else {
                    con = NO;
                    break;
                }
            }
        } else {
            con = NO;
        }
        if (con) {
            [result deleteCharactersInRange:NSMakeRange(result.length - 1, 1)];
            NSString *uniStr = [mixStr substringWithRange:NSMakeRange(index, 6)];
            NSString *plainStr = [self transToChinese:uniStr];
            [result appendString:plainStr];
            index = index + 5;
        }
    }
    return result;
}

- (NSString *)transToChinese:(NSString *)unicodeStr
{
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
    NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                          mutabilityOption:NSPropertyListImmutable
                                                                    format:NULL
                                                          errorDescription:NULL];
    
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}

#pragma mark NSTextViewDelegate

- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRanges:(NSArray *)affectedRanges replacementStrings:(NSArray *)replacementStrings
{
    if (replacementStrings.count == 1 && affectedRanges.count == 1) {
        NSRange range = [[affectedRanges lastObject] rangeValue];
        if (NSEqualRanges(NSMakeRange(_textView.string.length, 0), range)) {
            NSString *string = [replacementStrings lastObject];
            if ([string isEqualToString:@"\n"]) {
                NSString *text = [self transMixString:_textView.string];
                // 涉及到滚动的问题
                [_textView insertText:text replacementRange:NSMakeRange(0, 0)];
                if (text.length > _textView.string.length) {
                    [_textView insertText:text replacementRange:NSMakeRange(0, text.length)];
                } else {
                    [_textView insertText:text replacementRange:NSMakeRange(0, _textView.string.length)];
                }
                return NO;
            }
        }
    }
    return YES;
}


@end
